#include"purchased_goods_list.h"

/*初始化头节点*/
Cart_goods_list *cart_goods_init_list(void)
{
    Cart_goods_list *cart_goods = malloc(sizeof(Cart_goods_list));
    if(cart_goods!=NULL)
    {
        cart_goods->next = NULL;
    }
    return cart_goods;
}

/*创建新节点*/
Cart_goods_list * Cart_goods_new_node(Cart_goods data)
{
    Cart_goods_list * new =malloc(sizeof(Cart_goods_list));
    if(new!=NULL){
        strcpy(new->name,data.name);
        new->quantity = data.quantity;
        new->quantity_label = data.quantity_label;
        new->remainder = data.remainder;
        new->remainder_label = data.remainder_label;
        new->next = NULL;
    }
    return new;
}

/*判断链表是否为空*/
bool Cart_goods_is_empty(Cart_goods_list *list){
    return list->next==NULL;
}

/*插入节点*/
void Cart_goods_insert_node(Cart_goods_list * p,Cart_goods_list * new)
{
    if(p==NULL || new ==NULL){
        return;
    }
    new->next=p->next;
    p->next=new;
}

/*删除节点*/
bool cart_goods_remove_node(Cart_goods_list *mylist,Cart_goods_list *delete)
{
    if(Cart_goods_is_empty(mylist)){
        return false;
    }
    Cart_goods_list * p=mylist;
    while(p!=NULL && p->next!=delete){
        p=p->next;
    }
    if(p==NULL){
        return false;
    }

    p->next=delete->next;
    delete->next=NULL;
    return true;
}

/*根据name查找节点*/
Cart_goods_list* cart_goods_find_node_by_name(Cart_goods_list* mylist,char *name)
{
    if(Cart_goods_is_empty(mylist)){
        return NULL;
    }
    Cart_goods_list* p;
    for(p=mylist->next;p!=NULL;p=p->next){
        printf("p->name = %s\n",p->name);
        printf("name = %s\n",name);
        if(strcmp(p->name,name) == 0){
            printf("yes\n");
            break;
        }
    }
    return p;
}